package com.xuecheng.content.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.domain.mapper.TeachplanMapper;
import com.xuecheng.content.domain.model.dto.TeachplanDto;
import com.xuecheng.content.domain.service.TeachplanService;
import com.xuecheng.content.domain.model.entity.Teachplan;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Override
    public List<TeachplanDto> queryTreeNodes(Long courseId) {
        return teachplanMapper.queryTreeNodesByCourseId(courseId);
    }

    @Override
    public Integer queryMaxOrderBy(Teachplan teachplan) {
        return teachplanMapper.queryMaxOrderBy(teachplan.getParentid(), teachplan.getGrade(), teachplan.getCourseId());
    }

    @Override
    public void updateDownOrderBy(Teachplan teachplan) {
        lambdaUpdate().setSql("orderby = orderby - 1")
                .eq(Teachplan::getGrade, teachplan.getGrade())
                .eq(Teachplan::getParentid, teachplan.getParentid())
                .eq(Teachplan::getOrderby, teachplan.getOrderby() + 1)
                .update();
        lambdaUpdate().setSql("orderby = orderby + 1")
                .eq(Teachplan::getId, teachplan.getId())
                .update();
    }

    @Override
    public Integer queryMinOrderBy(Teachplan teachplan) {
        return teachplanMapper.queryMinOrderBy(teachplan.getParentid(), teachplan.getGrade(), teachplan.getCourseId());
    }

    @Override
    public void updateUpOrderBy(Teachplan teachplan) {
        lambdaUpdate().setSql("orderby = orderby + 1")
                .eq(Teachplan::getGrade, teachplan.getGrade())
                .eq(Teachplan::getParentid, teachplan.getParentid())
                .eq(Teachplan::getOrderby, teachplan.getOrderby() - 1)
                .update();
        lambdaUpdate().setSql("orderby = orderby - 1")
                .eq(Teachplan::getId, teachplan.getId())
                .update();
    }

    @Override
    public void saveTeachPlan(Teachplan teachplan) {
        Integer count = teachplanMapper.selectCount(new LambdaQueryWrapper<Teachplan>()
                .eq(Teachplan::getCourseId, teachplan.getCourseId())
                .eq(Teachplan::getParentid, teachplan.getParentid())
                .eq(Teachplan::getGrade, teachplan.getGrade()));
        teachplan.setOrderby(count + 1);
        teachplanMapper.insert(teachplan);
    }

    @Override
    public List<Long> queryNextIds(Teachplan teachplan) {
        return teachplanMapper.queryNextIds(teachplan.getParentid(), teachplan.getGrade(), teachplan.getCourseId(),
                teachplan.getOrderby());
    }

    @Override
    public void batchUpdateOrderBy(List<Long> teachPlanIds) {
        teachplanMapper.batchUpdateOrderBy(teachPlanIds);
    }
}
